//
//  LDJ_PageHeaderView.m
//  Pianke
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "LDJ_PageHeaderView.h"

@implementation LDJ_PageHeaderView


-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        
        [self creacteView];
    }
    return self;
}

-(void)creacteView{
    

    //滚动
    self.ringScrollView = [[UIScrollView alloc] init];
//    self.ringScrollView.contentSize = CGSizeMake(375 * 5 * EACH_WIDTH, 140 * EACH_HEIGHT);
    self.ringScrollView.backgroundColor = [UIColor grayColor];
    self.ringScrollView.pagingEnabled = 1;
//    [self.ringScrollView setContentOffset:CGPointMake(375 * EACH_HEIGHT, 0)];
    self.ringScrollView.delegate = self;
    [self addSubview:self.ringScrollView];
    
    [self.ringScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.right.bottom.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 140 * EACH_HEIGHT));
        
    }];
    
    //设置page
    self.page = [[UIPageControl alloc]init];
    self.page.backgroundColor = [UIColor clearColor];
    [self addSubview:self.page];
    //设置未选中点得颜色
    self.page.pageIndicatorTintColor = [UIColor lightGrayColor];
    //设置选中小点颜色
    self.page.currentPageIndicatorTintColor = [UIColor whiteColor];
    self.page.alpha = 0.6;
    
    self.page.currentPage = 0;
    //方法
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    
    [self.page mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(-5);
        make.bottom.mas_equalTo(self.ringScrollView.mas_bottom).offset(-5);
        make.size.mas_equalTo(CGSizeMake(120 * EACH_WIDTH, 30 * EACH_HEIGHT));
        
    }];
    
    //计时器
    [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(showAction) userInfo:nil repeats:YES];
    
}

#pragma mark - 计时器方法
-(void)showAction{
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [self.ringScrollView setContentOffset:CGPointMake(self.ringScrollView.contentOffset.x + SCREEN_WIDTH, 0) animated:YES];
        
    }];
    
    if(self.ringScrollView.contentOffset.x == SCREEN_WIDTH *self.showImgArray.count){
        self.ringScrollView.contentOffset = CGPointMake(0, 0);
    }
    self.page.currentPage = self.ringScrollView.contentOffset.x/SCREEN_WIDTH;
}







#pragma mark - 页码方法
-(void)pageAction:(UIPageControl *)page{
    
    [self.ringScrollView setContentOffset:CGPointMake(SCREEN_WIDTH * page.currentPage + 1, 0) animated:YES];
    
}



#pragma mark - 滑动停止时触发方法(轮播方法)
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (self.ringScrollView.contentOffset.x == 0) {
        
        //跳到倒数第3张图
        self.ringScrollView.contentOffset = CGPointMake(SCREEN_WIDTH *_showImgArray.count, 0);
    }
    if (self.ringScrollView.contentOffset.x == SCREEN_WIDTH * (_showImgArray.count + 1)){
        //跳到第2张
        self.ringScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    }
    
    self.page.currentPage = self.ringScrollView.contentOffset.x/SCREEN_WIDTH - 1;
    
}







#pragma mark - 重写数组 set方法
-(void)setShowImgArray:(NSMutableArray *)showImgArray{
    
    _showImgArray = showImgArray;
    
    //把数组 把图片扒出来
    NSMutableArray *array = [NSMutableArray array];
    //    NSMutableArray *urlArray = [NSMutableArray array];
    for (NSMutableDictionary *dic in showImgArray) {
        [array addObject:[dic objectForKey:@"img"]];
        //        [urlArray addObject:[dic objectForKey:@"url"]];
    }
    
    self.page.numberOfPages = showImgArray.count;
    
    //取出第一个和最后一个 分别插到数组的尾和头中
    NSString *string = [array objectAtIndex:0];
    [array insertObject:array.lastObject atIndex:0];
    [array addObject:string];
    
#pragma mark 设置轮播图页数
    self.ringScrollView.contentSize = CGSizeMake(self.ringScrollView.frame.size.width * array.count, self.ringScrollView.contentSize.height);//可能会出错
    
    //展示图
    for (int i = 0 ; i < array.count; i++) {
        
        UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.ringScrollView.frame.size.width * i, 0, self.ringScrollView.frame.size.width, self.ringScrollView.frame.size.height)];
        
        //赋值
        NSURL *name =[NSURL URLWithString: array[i]];
        [myImageView sd_setImageWithURL:name placeholderImage:[UIImage imageNamed:@"u=535380247,3773257076&fm=21&gp=0.jpg"]];
        
        [self.ringScrollView addSubview:myImageView];
        
    }
    //设置初始偏移量
    self.ringScrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
    
    
    
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
